import { _decorator, Component, Node, tween, UITransform, Vec3 } from 'cc';
import { PinActrion } from './PinActrion';
const { ccclass, property } = _decorator;

@ccclass('SlotAction')
export class SlotAction extends Component {
    start() {

    }

    update(deltaTime: number) {
        
    }

    putPin(pin: PinActrion): boolean {
        // 检查孔是否已满
        if (this.node.children.length >= 2) {
            return false;
        }
        let hasPin = false;
        // 检查是否有其他针脚在孔中
        for (let i = 0; i < this.node.children.length; i++) {
            let child = this.node.children[i];
            if (child.getComponent(PinActrion)) {
                hasPin = true;
                break;
            }
        }
        if(hasPin){
            return false;
        }

        pin.removeCollider()//去除碰撞检测
        
        // 将针脚添加到孔中
        // 世界坐标转本地坐标，再加移动动画
        let pinPos = pin.node.getWorldPosition();
        let locastPos = this.node.getComponent(UITransform).convertToNodeSpaceAR(pinPos);
        pin.node.setPosition(locastPos);
        this.node.addChild(pin.node);
        tween(pin.node).to(0.25, { position: Vec3.ZERO }).start();
        return true;
    }
}


